/**
 * 
 */
package pl.lodz.p.browarex.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import pl.lodz.p.browarex.dao.GenericDao;
import pl.lodz.p.browarex.model.Ingredient;
import pl.lodz.p.browarex.model.IngredientOrderInfo;
import pl.lodz.p.browarex.model.IngredientOrderRequest;

@Service
@Transactional(propagation = Propagation.REQUIRES_NEW)
@Qualifier("IngredientOrderRequestService")
public class IngredientOrderRequestService implements
		GenericService<IngredientOrderRequest, Long> {
	@Autowired
	private GenericDao<Ingredient, Long> ingredientDao;

	@Autowired
	@Qualifier("IngredientOrderRequestDao")
	private GenericDao<IngredientOrderRequest, Long> ingredientOrderRequestDao;

	@Override
	public void upsert(IngredientOrderRequest object) {
		List<IngredientOrderRequest> persistedObjects = findByProperty("orderId",object
				.getOrderId());
		if (persistedObjects.size() > 0) {
			IngredientOrderRequest ingredientOrderRequest = persistedObjects
					.get(0);
			ingredientOrderRequest.setOrderStatus(object.getOrderStatus());
			ingredientOrderRequestDao.update(ingredientOrderRequest);
		} else {
			ingredientOrderRequestDao.create(object);
		}

	}

	@Override
	public void delete(IngredientOrderRequest object) {
		ingredientOrderRequestDao.delete(object);
	}

	@Override
	public IngredientOrderRequest find(Long id) {
		return ingredientOrderRequestDao.findById(id);
	}

	@Override
	public List<IngredientOrderRequest> findByGuid(String guid) {
		return ingredientOrderRequestDao
				.findAllByProperty("orderId", guid);

	}

	@Override
	public List<IngredientOrderRequest> findByProperty(String property,
			Object value) {
		return ingredientOrderRequestDao.findAllByProperty(property, value);
	}

	@Override
	public List<IngredientOrderRequest> list() {
		return ingredientOrderRequestDao.findAll();
	}

}
